//
//  DownloadFileManager.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/10/23.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "DownloadFileManager.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CFileModel.h"
@implementation DownloadFileManager
//插入文件
-(BOOL)insertDownloadFile:(CFileModel *)fileModel
{
    if(fileModel == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"INSERT INTO DOWNLOADFILE(LocalFilePath, FileSize, DownloadSize, FileName, LocalCacheName, CreateDate, MessageIndex, GroupId,serverFileName, fromUserId,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? ,?,?,?,?)", M_STOREVALUE(fileModel.LocalFilePath), @(fileModel.FileSize), @(fileModel.DownloadSize), M_STOREVALUE(fileModel.FileName), M_STOREVALUE(fileModel.LocalCacheName), M_STOREVALUE(fileModel.CreateDate),  @(fileModel.MessageIndex), M_STOREVALUE(fileModel.GroupId),M_STOREVALUE(fileModel.serverFileName),M_STOREVALUE(fileModel.fromUserId),M_STOREVALUE(fileModel.yuLiuZiduan1),@(fileModel.yuLiuZiduan2),M_STOREVALUE(fileModel.yuLiuZiduan3)];
    }];
    return ret;
}
//批量插入文件
-(BOOL)batchInsertDownloadFile:(NSArray *)fileList
{
    if(fileList == nil || [fileList count] <= 0)
    {
        return NO;
    }
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CFileModel *fileModel in fileList)
        {
            ret = [db executeUpdate:@"INSERT INTO DOWNLOADFILE(LocalFilePath, FileSize, DownloadSize, FileName, LocalCacheName, CreateDate, MessageIndex, GroupId,serverFileName, fromUserId,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? ,?,?,?,?)", M_STOREVALUE(fileModel.LocalFilePath), @(fileModel.FileSize), @(fileModel.DownloadSize), M_STOREVALUE(fileModel.FileName), M_STOREVALUE(fileModel.LocalCacheName), M_STOREVALUE(fileModel.CreateDate),  @(fileModel.MessageIndex), M_STOREVALUE(fileModel.GroupId),M_STOREVALUE(fileModel.serverFileName),M_STOREVALUE(fileModel.fromUserId),M_STOREVALUE(fileModel.yuLiuZiduan1),@(fileModel.yuLiuZiduan2),M_STOREVALUE(fileModel.yuLiuZiduan3)];
            
            if(!ret)
            {
                *rollback = YES;
                break;
            }
        }
        
    }];
    return ret;
}
//删除文件
-(BOOL)delDownFiles:(CFileModel *)fileModel
{
    if(fileModel == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from DOWNLOADFILE where DownLoadIndex = ?", @(fileModel.FileIndex)];
    }];
    return ret;
}
//通过组id加载文件

- (BOOL)delDownFileWithServerFileName:(NSString *)ServerFileName
{
    if(ServerFileName == nil && ServerFileName.length <= 0)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from DOWNLOADFILE where serverFileName = ?", ServerFileName];
    }];
    return ret;

}


- (BOOL)delAllDownFile
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from DOWNLOADFILE"];
    }];
    return ret;

}
//通过组id加载文件
-(NSArray *)loadAllDownloadFiles
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    NSString *sql = nil;
    //    sql = [NSString stringWithFormat:@"select * from FILEMANAGER where GroupId = \'%@\'", groupId ];
    //   按照日期降序排列
    sql = [NSString stringWithFormat:@"select * from DOWNLOADFILE"];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CFileModel *fileModel = [[CFileModel alloc] init];
            fileModel.FileIndex = [result longLongIntForColumn:@"DownLoadIndex"];
            fileModel.LocalFilePath = [result stringForColumn:@"LocalFilePath"];
            fileModel.FileSize = [result longLongIntForColumn:@"FileSize"];
            fileModel.DownloadSize = [result longLongIntForColumn:@"DownloadSize"];
            fileModel.FileName = [result stringForColumn:@"FileName"];
            fileModel.LocalCacheName = [result stringForColumn:@"LocalCacheName"];
            fileModel.CreateDate = [result stringForColumn:@"CreateDate"];
            fileModel.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            fileModel.GroupId = [result stringForColumn:@"GroupId"];
            fileModel.fromUserId = [result stringForColumn:@"fromUserId"];
            fileModel.serverFileName = [result stringForColumn:@"serverFileName"];
            [array addObject:fileModel];
            fileModel = nil;
        }
        [result close];
    }];
    return array;

}

- (CFileModel *)loadFileWihtServerFileName:(NSString *)serverFileName
{
    if(M_ISSTRNULL(serverFileName) || serverFileName.length <= 0)
    {
        return nil;
    }
    __block CFileModel *fileModel = nil;
    NSString *sql = nil;
    //    sql = [NSString stringWithFormat:@"select * from FILEMANAGER where GroupId = \'%@\'", groupId ];
    //   按照日期降序排列
    sql = [NSString stringWithFormat:@"select * from DOWNLOADFILE where  serverFileName = \'%@\'", serverFileName];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            fileModel = [[CFileModel alloc] init];
            fileModel.FileIndex = [result longLongIntForColumn:@"DownLoadIndex"];
            fileModel.LocalFilePath = [result stringForColumn:@"LocalFilePath"];
            fileModel.FileSize = [result longLongIntForColumn:@"FileSize"];
            fileModel.DownloadSize = [result longLongIntForColumn:@"DownloadSize"];
            fileModel.FileName = [result stringForColumn:@"FileName"];
            fileModel.LocalCacheName = [result stringForColumn:@"LocalCacheName"];
            fileModel.CreateDate = [result stringForColumn:@"CreateDate"];
            fileModel.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            fileModel.GroupId = [result stringForColumn:@"GroupId"];
            fileModel.fromUserId = [result stringForColumn:@"fromUserId"];
            fileModel.serverFileName = [result stringForColumn:@"serverFileName"];
        }
        [result close];
    }];
    return fileModel;

}
@end
